package integration

import (
	"context"
	pb "gitee.com/micro-services/protocol/area/api"
	apb "gitee.com/micro-services/protocol/area/server"
)

const (
	firstLevel = 1
)

// 列表
func AreaList(ctx context.Context) ([]*pb.Area, error) {
	rsp, err := GetProxyManger().AreaClientProxy.AreaList(ctx, &apb.AreaListReq{})
	if err != nil {
		return nil, err
	}

	areas := make([]*pb.Area, 0)
	if rsp.AreaList != nil {
		for _, v := range rsp.GetAreaList() {
			areas = append(areas, &pb.Area{
				ID:       v.GetID(),
				ParentID: v.GetParentID(),
				Level:    v.GetLevel(),
				Name:     v.GetName(),
			})
		}
	}

	return convertArea(areas), nil
}

func convertArea(areas []*pb.Area) []*pb.Area {
	var node = make([]*pb.Area, 0)
	var otherNode = make([]*pb.Area, 0)
	for _, v := range areas {
		n := &pb.Area{
			ID:       v.ID,
			ParentID: v.ParentID,
			Level:    v.Level,
			Name:     v.Name,
		}
		// 找到最外层
		if v.Level == firstLevel {
			node = append(node, n)
		} else {
			otherNode = append(otherNode, n)
		}
	}

	for i := range node {
		node[i].SubArea = builderNode(otherNode, node[i].ID)
	}

	return node
}

func builderNode(nodes []*pb.Area, id int32) []*pb.Area {
	var temp = make([]*pb.Area, 0)
	for _, v := range nodes {
		if v.ParentID == id {
			temp = append(temp, &pb.Area{
				ID:       v.ID,
				ParentID: v.ParentID,
				Level:    v.Level,
				Name:     v.Name,
				SubArea:  builderNode(nodes, v.ID),
			})
		}
	}
	return temp
}

// 组装省市区数据
func assembly(areas []*apb.AreaList) []*pb.Area {
	list := make([]*pb.Area, 0)

	level1List := make([]*pb.Area, 0)
	level2List := make([]*pb.Area, 0)
	level3List := make([]*pb.Area, 0)
	for _, v := range areas {
		if v.Level == 1 {
			level1List = append(level1List, &pb.Area{
				ID:       v.ID,
				ParentID: v.ParentID,
				Level:    v.Level,
				Name:     v.Name,
			})
		}
		if v.Level == 2 {
			level2List = append(level2List, &pb.Area{
				ID:       v.ID,
				ParentID: v.ParentID,
				Level:    v.Level,
				Name:     v.Name,
			})
		}
		if v.Level == 3 {
			level3List = append(level3List, &pb.Area{
				ID:       v.ID,
				ParentID: v.ParentID,
				Level:    v.Level,
				Name:     v.Name,
			})
		}
	}

	for i, v := range level2List {
		for _, v3 := range level3List {
			if v.ID == v3.ParentID {
				level2List[i].SubArea = append(level2List[i].SubArea, v3)
			}
		}
	}

	for i, v := range level1List {
		for _, v2 := range level2List {
			if v.ID == v2.ParentID {
				level1List[i].SubArea = append(level1List[i].SubArea, v2)
			}
		}
	}

	list = level1List

	return list
}

// 创建商圈
func CreateCircle(ctx context.Context, r *pb.APICreateBusinessCircleReq) (*pb.APICreateBusinessCircleRsp, error) {
	rsp, err := GetProxyManger().AreaClientProxy.CreateCircle(ctx, &apb.CreateCircleReq{
		CircleName:      r.CircleName,
		CircleImage:     r.CircleImage,
		CircleBigImage:  r.CircleBigImage,
		CircleLongitude: r.CircleLongitude,
		CircleLatitude:  r.CircleLatitude,
		AreaID:          r.AreaID,
		DetailAddress:   r.DetailAddress,
		IsHot:           r.IsHot,
	})
	if err != nil {
		return nil, err
	}
	return &pb.APICreateBusinessCircleRsp{
		RetCode: rsp.RetCode,
		Msg:     rsp.Msg,
	}, nil
}

// 删除商圈
func DeleteCircle(ctx context.Context, r *pb.APIDeleteBusinessCircleReq) (*pb.APIDeleteBusinessCircleRsp, error) {
	rsp, err := GetProxyManger().AreaClientProxy.DeleteCircle(ctx, &apb.DeleteCircleReq{
		BusinessCircleID: r.BusinessCircleID,
	})
	if err != nil {
		return nil, err
	}

	return &pb.APIDeleteBusinessCircleRsp{
		RetCode: rsp.RetCode,
		Msg:     rsp.Msg,
	}, nil
}

// 商圈详情
func BatchCircleInfo(ctx context.Context, r *pb.APIBatchBusinessCircleInfoReq) (*pb.APIBatchBusinessCircleInfoRsp, error) {
	rsp, err := GetProxyManger().AreaClientProxy.BatchCircleInfo(ctx, &apb.BatchCircleInfoReq{
		BusinessCircleID: r.BusinessCircleID,
	})
	if err != nil {
		return nil, err
	}

	return &pb.APIBatchBusinessCircleInfoRsp{
		BusinessCircleInfo: rsp.BusinessCircleInfo,
	}, nil
}

// 商圈搜索
func SearchCircle(ctx context.Context, r *pb.APISearchBusinessCircleReq) (*pb.APISearchBusinessCircleRsp, error) {
	rsp, err := GetProxyManger().AreaClientProxy.SearchCircle(ctx, &apb.SearchCircleReq{
		AreaID:     r.AreaID,
		CircleName: r.CircleName,
		IsHot:      r.IsHot,
		Offset:     r.Offset,
		Limit:      r.Limit,
	})
	if err != nil {
		return nil, err
	}

	return &pb.APISearchBusinessCircleRsp{
		BusinessCircleInfo: rsp.BusinessCircleInfo,
		Count:              rsp.Count,
	}, nil
}
